package com.hzdh.account.controller.user;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hzdh.account.commom.Message;
import com.hzdh.account.entity.login.User;
import com.hzdh.account.request.login.UserRequest;
import com.hzdh.account.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	private static Logger logger = LoggerFactory.getLogger(UserController.class); 
	
	@Autowired
	private UserService userService;
	
	/**
	 * 用户信息页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/info")
	public String infoView(HttpServletRequest request) {
		logger.info("request param = {}", request.getSession().getAttribute("userSession"));
		return "user/info";
	}

	/**
	 * 新增用户页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	public String addView(HttpServletRequest request) {
		logger.info("request param = {}", request.getSession().getAttribute("userSession"));
		request.setAttribute("user", (User)request.getSession().getAttribute("userSession"));
		return "user/add";
	}

	/**
	 * 用户查询
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query")
	public Object queryUser(HttpServletRequest request, UserRequest userRequest) {
		List<User> user = userService.selectUser(userRequest);
		return null;
	}

	/**
	 * 用户新增
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add/user")
	public Message addUser(HttpServletRequest request,UserRequest userRequest) {

		Message message = null;
		try {
			userService.addUserInfo(userRequest);
			message = new Message(0, "用户添加成功!");
		} catch (Exception e) {
			logger.info("exception ={}", e.getMessage());
			message = new Message(0, "用户添加失败!");
		}
		return message;
	}

	/**
	 * 用户列表
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query/user/list")
	public Message userList(@RequestParam Map<String, String> paramMap) {
		
		Message message = null;
		try {
			PageInfo<User> pageInfo = userService.getUserPage(paramMap);
			if (pageInfo != null) {
				if (pageInfo.getTotal() > 0) {
					message = new Message<>(0, "获取数据成功!", pageInfo.getTotal(), pageInfo.getList());
				} else {
					message = new Message<>(0, "无数据!", null, null);
				}
			} else {
				message = new Message<>(0, "参数异常!", null, null);
			}
		} catch (Exception e) {
			logger.info("exception = {}", e.getStackTrace() + e.getMessage());
			message = new Message(400,"查询失败!",0L,null);
		}
		return message;
	}
	
}
